﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.master
{
    public partial class customer : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var authsvc = new AuthService();
            try
            {
                divLogin.Visible = (Session["authinfo"] == null);
                liAccount.Visible = (Session["authinfo"] != null);
                divAccount.Visible = (Session["authinfo"] != null);
                if (Session["authinfo"] != null)
                {
                    var authinfo = (Account)Session["authinfo"];
                    const string gravatarRoot = "http://www.gravatar.com/avatar/";
                    string emailHash = Checksum.MD5(authinfo.Email.Trim());
                    imgProfile.ImageUrl = gravatarRoot + emailHash + "?s=120";
                    pRole.InnerText = GetRoleName(authinfo.Role);
                    pFullName.InnerText = authinfo.FullName;
                    pUserName.InnerText = authinfo.UserName;
                }
            }
            catch (Exception) { }
        }

        protected string GetRoleName(short role)
        {
            switch (role)
            {
                case 0: return "Customer";
                case 1: return "Employee";
                case 2: return "Manager";
            }
            return String.Empty;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var authinfo = new Account();
            var authsvc = new AuthService();
            authinfo.UserName = txtUsername.Text.Trim();
            authinfo.Password = Checksum.MD5(txtPassword.Text.Trim());
            authinfo.Role = 2;
            var check = authsvc.CheckLogin(authinfo);
            if (check == true)
            {
                Session["authinfo"] = authinfo;
                switch (authinfo.Role)
                {
                    case 0:
                        Response.Redirect("~/default.aspx");
                        break;
                    case 1:
                        Response.Redirect("~/employee/default.aspx");
                        break;
                    case 2:
                        Response.Redirect("~/manager/default.aspx");
                        break;
                }
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }
        }
    }
}